package com.cyz.admin.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cyz.admin.service.ShopTypeService;
import com.cyz.common.bean.ShopType;
import com.cyz.common.dto.ShopTypeDto;
import com.cyz.common.enums.MyEnums;
import com.cyz.common.mapper.ShopTypeMapper;
import com.cyz.common.utils.BeanCopyUtil;
import com.cyz.common.utils.R;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * 管理端付费课分类接口实现类
*/
@Service
public class ShopTypeServiceImpl extends ServiceImpl<ShopTypeMapper, ShopType>
    implements ShopTypeService {

    @Override
    public R queryShopTypes() {
        //查询数据
        List<ShopType> list = list();
        //错误判断
        if(list.isEmpty()){
            return R.error(MyEnums.QUERY_FAIL);
        }
        return R.success(list);
    }

    @Override
    public R deleteShopTypes(Integer[] ids) {
        //删除数据
        boolean flag = removeByIds(Arrays.asList(ids));
        //错误判断
        if(flag){
            return queryShopTypes();
        }
        return R.error(MyEnums.DELETE_FAIL);
    }

    @Override
    public R addShopType(ShopTypeDto shopTypeDto) {
        //dto转化
        ShopType shopType = BeanCopyUtil.copyBean(shopTypeDto, ShopType.class);
        //添加数据
        boolean flag = save(shopType);
        //错误判断
        if(flag){
            return queryShopTypes();
        }
        return R.error(MyEnums.ADD_FAIL);
    }

    @Override
    public R updateShopType(ShopTypeDto shopTypeDto) {
        //dto转化
        ShopType shopType = BeanCopyUtil.copyBean(shopTypeDto, ShopType.class);
        //添加数据
        boolean flag = updateById(shopType);
        //错误判断
        if(flag){
            return queryShopTypes();
        }
        return R.error(MyEnums.UPDATE_FAIL);
    }
}




